/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.banner.anim;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.components.Component;

/**
 * 动画基类
 *
 * @since 2021-05-06
 */
public abstract class BaseAnimator {
    protected AnimatorGroup mAnimatorSet = new AnimatorGroup();
    private static final int defaultTime = 500;
    protected long mDuration = defaultTime;
    private Animator.TimelineCurve mInterpolator;
    private long mDelay;
    private AnimatorListener mListener;

    /**
     * 设置动画
     *
     * @param view
     */
    public abstract void setAnimation(Component view);

    /**
     * 开始动画
     *
     * @param view
     */
    protected void start(final Component view) {
        setAnimation(view);

        mAnimatorSet.setDuration(mDuration);
        if (mInterpolator != null) {
            mAnimatorSet.setCurve(mInterpolator);
        }

        if (mDelay > 0) {
            mAnimatorSet.setDelay(mDelay);
        }

        if (mListener != null) {
            mAnimatorSet.setLoopedListener(new Animator.LoopedListener() {
                @Override
                public void onRepeat(Animator animator) {
                    mListener.onAnimationRepeat(animator);
                }
            });
            mAnimatorSet.setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                    mListener.onAnimationStart(animator);
                }

                @Override
                public void onStop(Animator animator) {
                }

                @Override
                public void onCancel(Animator animator) {
                    mListener.onAnimationCancel(animator);
                }

                @Override
                public void onEnd(Animator animator) {
                    mListener.onAnimationEnd(animator);
                }

                @Override
                public void onPause(Animator animator) {
                }

                @Override
                public void onResume(Animator animator) {
                }
            });
        }
        mAnimatorSet.start();
    }

    /**
     * 重置View动画
     *
     * @param view
     */
    public void reset(Component view) {
        view.setAlpha(1);
        view.setScaleX(1.0f);
        view.setScaleY(1.0f);
        view.setTranslationX(0);
        view.setTranslationY(0);
        view.setRotation(0);
    }

    /**
     * 设置动画时间
     *
     * @param duration
     * @return 返回当前BaseAnimator
     */
    public BaseAnimator duration(long duration) {
        this.mDuration = duration;
        return this;
    }

    /**
     * 设置延时时间
     *
     * @param delay
     * @return 返回当前BaseAnimator
     */
    public BaseAnimator delay(long delay) {
        this.mDelay = delay;
        return this;
    }

    /**
     * 设置插值器
     *
     * @param interpolator
     * @return 返回当前BaseAnimator
     */
    public BaseAnimator interpolator(Animator.TimelineCurve interpolator) {
        this.mInterpolator = interpolator;
        return this;
    }

    /**
     * 设置动画监听
     *
     * @param listener
     * @return 返回当前BaseAnimator
     */
    public BaseAnimator listener(AnimatorListener listener) {
        this.mListener = listener;
        return this;
    }

    /**
     * 开始播放动画
     *
     * @param view
     */
    public void playOn(Component view) {
        start(view);
    }

    /**
     * 动画监听接口
     *
     * @since 2021-05-06
     */
    public interface AnimatorListener {
        /**
         * 动画开始
         *
         * @param animator
         */
        void onAnimationStart(Animator animator);

        /**
         * 动画重复
         *
         * @param animator
         */
        void onAnimationRepeat(Animator animator);

        /**
         * 动画结束
         *
         * @param animator
         */
        void onAnimationEnd(Animator animator);

        /**
         * 动画取消
         *
         * @param animator
         */
        void onAnimationCancel(Animator animator);
    }
}
